package com.whpost.service.tools;


import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import java.security.MessageDigest;


public class Url 
{
	private final static int ITERATIONS = 60;

	public static String encrypt(String plainText) throws Exception 
	{
		String key="whpost&*masd=12zmjHYGJ123=10990121129219021212121";
		try 
		{
			byte[] salt = new byte[8];
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(key.getBytes());
			byte[] digest = md.digest();
			for (int i = 0; i < 8; i++) 
			{
				salt[i] = digest[i];
			}
			PBEKeySpec pbeKeySpec = new PBEKeySpec(key.toCharArray());
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
			SecretKey skey = keyFactory.generateSecret(pbeKeySpec);
			PBEParameterSpec paramSpec = new PBEParameterSpec(salt, ITERATIONS);
			Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
			cipher.init(Cipher.ENCRYPT_MODE, skey, paramSpec);
			byte[] cipherText = cipher.doFinal(plainText.getBytes());
			String saltString = new String(Base64.encode(salt));
			String ciphertextString = new String(Base64.encode(cipherText));
			return saltString + ciphertextString;
		}	 
		catch (Exception e) 
		{
			throw new Exception("Encrypt Text Error:" + e.getMessage(), e);
		}
	}

	public static String decrypt(String encryptTxt) throws Exception 
	{
		int saltLength = 12;
		String key="whpost&*masd=12zmjHYGJ123=10990121129219021212121";
		try 
		{
			String salt = encryptTxt.substring(0, saltLength);
			//System.out.println("加密后："+encryptTxt);
			//System.out.println("加密后长度："+encryptTxt.length());
			String ciphertext = encryptTxt.substring(saltLength, encryptTxt.length());
			byte[] saltarray = Base64.decode(salt.getBytes());
			byte[] ciphertextArray = Base64.decode(ciphertext.getBytes());
			PBEKeySpec keySpec = new PBEKeySpec(key.toCharArray());
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
			SecretKey skey = keyFactory.generateSecret(keySpec);
			PBEParameterSpec paramSpec = new PBEParameterSpec(saltarray,ITERATIONS);
			Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
			cipher.init(Cipher.DECRYPT_MODE, skey, paramSpec);
			byte[] plaintextArray = cipher.doFinal(ciphertextArray);
			return new String(plaintextArray);
		} 
		catch (Exception e) 
		{
			throw new Exception("decrypt Text Error:" + e.getMessage(), e);
		}
	}
	
	public static void test(Integer str)
	{
		System.out.println("str:"+str);
	}
	
	public static void main(String[] args)
	{
		String encryptTxt = "";
		String plainTxt = "15972955564";
		try 
		{

			encryptTxt = encrypt(plainTxt);
			plainTxt = decrypt(java.net.URLDecoder.decode("%2FjQgBM8DQso%3D2ZkuRLKERPY%3D","utf-8"));



			System.out.println("加密后："+encryptTxt);
			System.out.println("解密后:"+plainTxt);

		} 
		catch (Exception e) 
		{
			e.printStackTrace();
			System.exit(-1);
		}
	 }
		
}




